package com.cmrx.model.controller;

import com.alibaba.cloudapi.sdk.core.model.ApiResponse;
import com.cmrx.model.util.PropertyUtil;
import com.cmrx.model.util.SyncApiListClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("api")
@Slf4j
@Api(value = "/api",description = "一标六实Api")
public class ApiController {
    private static final String APP_KEY = PropertyUtil.getValue("app_key"); //提供的应用key
    private static final String APP_SECRET = PropertyUtil.getValue("app_secret"); //提供的应用secret


    @RequestMapping(value = "imap/stree/view",method = RequestMethod.GET)
    @ApiOperation(value = "IMapStreetView.js接口")
    public String getIMapStreetViewJs(){
        String apiPath = "/jsmap/1.0/flash/IMapStreetView.js";
        return HttpGet(apiPath);
    }


    @RequestMapping(value ="/main",method = RequestMethod.GET)
    @ApiOperation(value = "main.js接口")
    public String getMainJs(){
        String apiPath = "/jsmap/1.0/main.js";
        return HttpGet(apiPath);
    }

    @RequestMapping(value = "imap",method = RequestMethod.GET)
    @ApiOperation(value = "IMap.css接口")
    public String getIMapCss(){
        String apiPath = "/jsmap/1.0/IMap.css";
        return HttpGet(apiPath);
    }


    @ApiOperation(value="坐标经纬度转换")
    @ApiImplicitParam(name = "coords", value = "坐标经纬度，逗号分隔", required = true, dataType = "String")
    @RequestMapping(value = "test",method = RequestMethod.GET)
    public String getAuth(@PathVariable String coords){
        String apiPath = "/as/coord/convert?coords="+coords+"&ak="+PropertyUtil.getValue("app_key");
        return HttpGet(apiPath);
    }


    public String HttpGet(String apiPath){
        SyncApiListClient syncClient = SyncApiListClient.newBuilder()
                .appKey(APP_KEY)
                .appSecret(APP_SECRET)
                .build();

        Map<String, String> headerParams = new HashMap<String, String>();
        headerParams.put("X-Ca-Stage","RELEASE");
        headerParams.put("Content-Type","application/json; charset=UTF-8");
        headerParams.put("gateway_channel","http");
        headerParams.put("X-Ca-Request-Mode","debug");

        ApiResponse formResponse= syncClient.APIList(headerParams,apiPath);
        String encodedText = "";
        try {
            byte[] body = formResponse.getBody();
            encodedText = new String(body);
        }catch (Exception e){
            e.printStackTrace();
        }
        return encodedText;
    }


}
